module ScheduleHelper
  
  #Returns an html string indicating the status of the current student's ability to register for a semester.
  #The returned string is a span enclosing a statement.
  #The span's class is either "registration_closed" or "registration_open" and the message
  #is either "Registration open", "Registration closed", or "Registration not yet opened"
  def html_registration_state_for(semester)
    #possible strings
    not_yet = '<span class="registration_closed">(Registration not yet open)</span>'
    closed = '<span class="registration_closed">(Registration closed)</span>'
    open = '<span class="registration_open">(Registration open)</span>'

    
    #No registrations defined for semester
    if semester.registrations == []
      return not_yet
    end
    
    student = Student.find(session['student_id'])
    #At least one registration is defined
    reg = student.current_open_registration
    if reg.nil?
      return not_yet
    elsif reg.semester <= semester and reg.semester >= semester
      return open
    elsif reg.semester <= semester
      return closed
    elsif reg.semester <= semester
      return not_yet
    end
  end
  
  #Javascript helpers
  def semester_droppable(opt_hash)
    semester = Semester.find(opt_hash[:semester])
    student = Student.find(opt_hash[:student_id])
    other_semesters_element_ids = student.future_semesters_except(semester).collect {|e| "\"semester_"+e.element_id + '"'}.join(', ')
    ondrop_function = "function(element){new Ajax.Request('/schedule/move_course?semester_id=#{semester.id}&student_id=#{opt_hash[:student_id]}', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURI(element.id)})}"
    
    "Droppables.add(\"#{semester.element_id}\", {accept: [#{other_semesters_element_ids}, 'not_taken'], hoverclass:'dropping', onDrop:#{ondrop_function}, onHover:hovering});"
  end
  
  def course_draggable(opt_hash)
    #revert_function = "function(element){return $(\"#{opt_hash[:semester].element_id}\") == element.element.up('ul')}"
    #onEnd_function = "function(element, event){alert($(\"#{opt_hash[:semester].element_id}\").id +\", \"+ element.element.up('ul').id + \", \"+event.elements)}"
    "new Draggable('#{opt_hash[:course_number]}', {revert:revert_course});"
  end
  
  def elective_info(bool)
    return "<span class=\"tiny\">(technical elective)</span>" if bool
    return ''
  end
  
  def section_time_conflict_html(conflicting)
    return "<span class=\"conflict_text\">(time conflict with #{conflicting.course.number})</span>" if conflicting.class == Section
    return ''
  end
  
  def section_time_conflict_html_with_selection(conflicting, chosen_section, current_section, course_element_id, course)
    return link_to_remote("(select)", :update => course_element_id, :url => {:action => 'choose_section', :chosen_section_id => chosen_section.id, :current_section_id => current_section.id, :course_id => course.id}) if current_section.course == Course.with_number("HIST2112")
    if section_time_conflict_html(conflicting) == ''
      return link_to_remote("(select)", :update => course_element_id, :url => {:action => 'choose_section', :chosen_section_id => chosen_section.id})
    end
  end
end